<?php

if(!defined('IN_JISHIGOU'))
{
exit('invalid request');
}
class ModuleObject extends MasterObject
{
function ModuleObject($config)
{
$this->MasterObject($config);
$this->Execute();
}
function Execute()
{
switch($this->Code)
{
case 'view':		      
case 'show':
{
$this->Show();
break;
}
case 'fans':
{
$this->Fans();
break;
}
case 'follow':
{
$this->Follow();
break;
}
case 'follow_new':
case 'follownew':
case 'new_follow':
case 'newfollow':
{
$this->FollowNew();
break;
}
case 'follow_del':
case 'followdel':
case 'del_follow':
case 'delfollow':
{
$this->FollowDel();
break;
}
case 'follow_show':
case 'followshow':
case 'show_follow':
case 'showfollow':
{
$this->FollowShow();
break;
}
case 'my_topic':    
case 'mytopic':    
case 'topic':
{
$this->Topic();
break;
}
case 'my_friend_topic':
case 'myfriendtopic':
{
$this->MyFriendTopic();
break;
}
case 'my_favorite':
case 'myfavorite':
{
$this->MyFavorite();
break;
}
case 'favorite_my':
case 'favoritemy':
{
$this->FavoriteMy();
break;
}
case 'my_comment':
case 'mycomment':
{
$this->MyComment();
break;
}
case 'comment_my':
case 'commentmy':
{
$this->CommentMy();
break;
}
case 'at_my':
case 'atmy':
{
$this->AtMy();
break;
}
case 'my_pm':
case 'mypm':
case 'pm':
{
$this->PmList();
break;
}
case 'pm_sent':
case 'pmsent':
{
$this->PmSent();
break;
}
case 'pm_new':
case 'pmnew':
{
$this->PmNew();
break;
}
default :
{
$this->Main();
break;
}
}
}
function Main()
{
api_output('api is ok');
}
function Show()
{
$uid = max(0,(int) ($this->Inputs['uid'] ?$this->Inputs['uid'] : $this->user['uid']));
$user = $this->_init_user($uid);
api_output($user);
}
function Fans()
{
$uid = max(0,(int) ($this->Inputs['uid'] ?$this->Inputs['uid'] : $this->user['uid']));
$user = $this->_init_user($uid);
$page_arr = $this->_page($user['fans_count']);
$sql = "select `uid` from `".TABLE_PREFIX."buddys` where `buddyid`='{$user['uid']}' order by `id` desc limit {$page_arr[offset]},{$page_arr[count]}";
$query = $this->DatabaseHandler->Query($sql);
$uids = array();
while ($row = $query->GetRow())
{
$uids[$row['uid']] = $row['uid'];
}
$return = array();
if($uids)
{
$return = $page_arr;
$return['users'] = $this->_user($uids);
}
api_output($return);
}
function Follow()
{
$uid = max(0,(int) ($this->Inputs['uid'] ?$this->Inputs['uid'] : $this->user['uid']));
$user = $this->_init_user($uid);
$page_arr = $this->_page($user['follow_count']);
$sql = "select `uid` from `".TABLE_PREFIX."buddys` where `uid`='{$user['uid']}' order by `id` desc limit {$page_arr[offset]},{$page_arr[count]}";
$query = $this->DatabaseHandler->Query($sql);
$uids = array();
while ($row = $query->GetRow())
{
$uids[$row['uid']] = $row['uid'];
}
$return = array();
if($uids)
{
$return = $page_arr;
$return['users'] = $this->_user($uids);
}
api_output($return);
}
function FollowNew()
{
$uid = max(0,(int) $this->Inputs['uid']);
$user = $this->_init_user($uid);
if($uid!=$this->user['uid'])
{
$row = $this->DatabaseHandler->FetchFirst("select * from `".TABLE_PREFIX."buddys` where `uid`='{$this->user['uid']}' and `buddyid`='{$uid}' limit 1");
if(!$row)
{
$timestamp = time();
$this->DatabaseHandler->Query("insert into `".TABLE_PREFIX."buddys` (`uid`,`buddyid`,`dateline`,`buddy_lastuptime`) values ('{$this->user['uid']}','{$uid}','{$timestamp}','{$timestamp}')");
update_my_fans_follow_count($this->user['uid']);
update_my_fans_follow_count($uid);
}
}
api_output('follownew is ok');
}
function FollowDel()
{
$uid = max(0,(int) $this->Inputs['uid']);
$user = $this->_init_user($uid);
if($uid!=$this->user['uid'])
{
$row = $this->DatabaseHandler->FetchFirst("select * from `".TABLE_PREFIX."buddys` where `uid`='{$this->user['uid']}' and `buddyid`='{$uid}' limit 1");
if($row)
{
$this->DatabaseHandler->Query("delete from `".TABLE_PREFIX."buddys` where `uid`='{$this->user['uid']}' and `buddyid`='{$uid}' limit 1");
update_my_fans_follow_count($this->user['uid']);
update_my_fans_follow_count($uid);
}
api_output('followdel is ok');
}
}
function FollowShow()
{
$source_id = max(0,(int) ($this->Inputs['source_id'] ?$this->Inputs['source_id'] : $this->user['uid']));
if(!$source_id)
{
api_error('source_id is empty',107);
}
$source_user = $this->_user($source_id);
if(!$source_user)
{
api_error('source_id is invalid',108);
}
$target_id = max(0,(int) ($this->Inputs['target_id']));
if(!$target_id)
{
api_error('target_id is empty',109);
}
$target_user = $this->_user($target_id);
if($source_id==$target_id ||!$target_user)
{
api_error('target_id is invalid',110);
}
$return = array();
$return['source'] = array(
'uid'=>$source_user['uid'],
'username'=>$source_user['username'],
'nickname'=>$source_user['nickname'],
'following'=>false,
'followed_by'=>false,
);
$return['target'] = array(
'uid'=>$target_user['uid'],
'username'=>$target_user['username'],
'nickname'=>$target_user['nickname'],
'following'=>false,
'followed_by'=>false,
);
$row = $this->DatabaseHandler->FetchFirst("select * from `".TABLE_PREFIX."buddys` where `uid`='{$source_id}' and `buddyid`='{$target_id}' limit 1");
if($row)
{
$return['target']['followed_by'] = $return['source']['following'] = true;
}
$row = $this->DatabaseHandler->FetchFirst("select * from `".TABLE_PREFIX."buddys` where `uid`='{$target_id}' and `buddyid`='{$source_id}' limit 1");
if($row)
{
$return['target']['following'] = $return['source']['followed_by'] = true;
}
api_output($return);
}
function Topic()
{
$uid = max(0,(int) ($this->Inputs['uid'] ?$this->Inputs['uid'] : $this->user['uid']));
$user = $this->_init_user($uid);
$sql_wheres = array();
$sql_wheres[] = "`uid`='{$uid}'";
$id_max = max(0,(int) $this->Inputs['id_max']);
if($id_max)
{
$sql_wheres[] = "`tid`<='$id_max'";
}
$id_min = max(0,(int) $this->Inputs['id_min']);
if($id_min)
{
$sql_wheres[] = "`tid`>'$id_min'";
}
$sql_where = " where ".implode(" and ",$sql_wheres);
$sql = "select count(*) as count from ".TABLE_PREFIX."topic $sql_where";
$row = $this->DatabaseHandler->FetchFirst($sql);
$return = array();
if($row['count'])
{
$return = $this->_page($row['count']);
$return['topics'] = $this->_topic(" $sql_where order by `dateline` desc limit {$return[offset]},{$return[count]} ");
}
api_output($return);
}
function MyFriendTopic()
{
$uid = max(0,(int) $this->user['uid']);
$user = $this->_init_user($uid);
$sql = "select `buddyid` as `uid` from `".TABLE_PREFIX."buddys` where `uid`='{$uid}'";
$query = $this->DatabaseHandler->Query($sql);
$uids = array();
while($row = $query->GetRow())
{
$uids[$row['uid']] = $row['uid'];
}
$uids[$uid] = $uid;
$sql_wheres = array();
$return = array();
if($uids)
{
$sql_wheres[] = "`uid` in ('".implode("','",$uids)."')";
}
$sql_where = ($sql_wheres ?" where ".(implode(" and ",$sql_wheres)) : null);
$row = $this->DatabaseHandler->FetchFirst("select count(*) as `count` from ".TABLE_PREFIX."topic $sql_where ");
if($row['count'])
{
$return = $this->_page($row['count']);
$sql_where .= " order by `dateline` desc limit {$return[offset]},{$return[count]} ";
$return['topics'] = $this->_topic($sql_where);
}
api_output($return);
}
function MyFavorite()
{
$uid = max(0,(int) $this->user['uid']);
$user = $this->_init_user($uid);
$return = array();
$row = $this->DatabaseHandler->FetchFirst("select count(*) as `count` from `".TABLE_PREFIX."topic_favorite` TF where TF.uid='{$uid}'");
if($row['count'])
{
$return = $this->_page($row['count']);
$sql = "select TF.dateline as favorite_time , T.* from `".TABLE_PREFIX."topic_favorite` TF left join `".TABLE_PREFIX."topic` T on T.tid=TF.tid where TF.uid='{$uid}' order by TF.id desc limit {$return['offset']},{$return['count']}";
$query = $this->DatabaseHandler->Query($sql);
$topic_list = array();
while ($row = $query->GetRow())
{
if($row['tid']<1) 
{
continue;
}
$row['favorite_time'] = my_date_format2($row['favorite_time']);
$row = $this->TopicLogic->Make($row);
$topic_list[$row['tid']] = $row;
}
$return['topics'] = $topic_list;
}
api_output($return);
}
function FavoriteMy()
{
$uid = max(0,(int) $this->user['uid']);
$user = $this->_init_user($uid);
$return = array();
$row = $this->DatabaseHandler->FetchFirst("select count(*) as `count` from `".TABLE_PREFIX."topic_favorite` TF where TF.tuid='{$uid}'");
if($row['count'])
{
$return = $this->_page($row['count']);
$query = $this->DatabaseHandler->Query("select TF.dateline as favorite_time , TF.uid as fuid , T.* from `".TABLE_PREFIX."topic_favorite` TF left join `".TABLE_PREFIX."topic` T on T.tid=TF.tid where TF.tuid='{$uid}' order by TF.id desc limit {$return['offset']},{$return['count']}");
while ($row = $query->GetRow())
{
if($row['tid']<1) continue;
$row['favorite_time'] = my_date_format2($row['favorite_time']);
$row = $this->TopicLogic->Make($row);
$row['favorite_members'] = $this->TopicLogic->GetMember($row['fuid'],"`uid`,`ucuid`,`username`,`nickname`,`face_url`,`face`,`validate`");
$topic_list[$row['tid']] = $row;
}
$return['topics'] = $topic_list;
}
api_output($return);
}
function MyComment()
{
$uid = max(0,(int) $this->user['uid']);
$user = $this->_init_user($uid);
$return = array();
$row = $this->DatabaseHandler->FetchFirst("select count(*) as `count` from ".TABLE_PREFIX."topic where `uid` = '$uid' and `type` in ('both','reply')");
if($row['count'])
{
$return = $this->_page($row['count']);
$return['topics'] = $this->_topic(" where `uid` = '$uid' and `type` in ('both','reply') order by `dateline` desc limit {$return['offset']},{$return['count']} ");
}
api_output($return);
}
function CommentMy()
{
$uid = max(0,(int) $this->user['uid']);
$user = $this->_init_user($uid);
$return = array();
$row = $this->DatabaseHandler->FetchFirst("select count(*) as `count` from ".TABLE_PREFIX."topic where `touid`='{$uid}' and `type` in ('both','reply') ");
if($row['count'])
{
$return = $this->_page($row['count']);
$return['topics'] = $this->_topic(" where `touid`='{$uid}' and `type` in ('both','reply') order by `dateline` desc limit {$return['offset']},{$return['count']} ");
}
api_output($return);
}
function AtMy()
{
$uid = max(0,(int) $this->user['uid']);
$user = $this->_init_user($uid);
$return = array();
$sql = "select * from `".TABLE_PREFIX."topic_mention` where `uid`='{$uid}'";
$query = $this->DatabaseHandler->Query($sql);
while ($row = $query->GetRow())
{
$topic_ids[$row['tid']] = $row['tid'];
}
$topic_ids_count = count($topic_ids);
if($topic_ids_count)
{
$return = $this->_page($topic_ids_count);
$return['topics'] = $this->_topic(" where `tid` in ('".implode("','",$topic_ids)."') order by `dateline` desc limit {$return['offset']},{$return['count']} ");
}
api_output($return);
}
function PmList()
{
$uid = max(0,(int) $this->user['uid']);
$user = $this->_init_user($uid);
$filter = ($this->Inputs['filter']=='newpm') ?" AND `new`>0 ": '';
$return = array();
$row = $this->DatabaseHandler->FetchFirst("SELECT count(*) as `count` from ".TABLE_PREFIX.'pms'." WHERE msgtoid =".$uid." $filter AND `folder`='inbox' AND `delstatus`!='2'");
if($row['count'])
{
$return = $this->_page($row['count']);
$query = $this->DatabaseHandler->Query("select * from ".TABLE_PREFIX."pms WHERE msgtoid =".$uid." $filter AND `folder`='inbox' AND `delstatus`!='2' order by `pmid` desc limit {$return['offset']},{$return['count']}");
$pm_list=array();
while($row=$query->GetRow())
{
$row['send_time'] = my_date_format($row['dateline'],"Y-m-d H:i");
$row['user'] = $row['msgto'];
$row['user_id'] = $row['msgtoid'];
$row['id'] = $row['pmid'];
$row['nickname'] = $row['msgnickname'];
$pm_list[]=$row;
}
$return['pms'] = $pm_list;
}
api_output($return);
}
function PmSent()
{
$uid = max(0,(int) $this->user['uid']);
$user = $this->_init_user($uid);
$return = array();
$row = $this->DatabaseHandler->FetchFirst("SELECT count(*) as `count` from ".TABLE_PREFIX.'pms'." WHERE `msgfromid`=".$uid." AND `folder`='inbox' AND `delstatus`!='1'");
if($row['count'])
{
$return = $this->_page($row['count']);
$query = $this->DatabaseHandler->Query("SELECT p.*, m.`uid`,m.`nickname`,m.`username`, m.uid AS msgto FROM ".TABLE_PREFIX.'pms'." p LEFT JOIN ".TABLE_PREFIX.'members'." m ON m.uid=p.msgtoid WHERE p.msgfromid=".$uid." AND p.folder='inbox' AND delstatus!='1' ORDER BY p.pmid DESC limit {$return['offset']},{$return['count']}");
$pm_list=array();
while($row=$query->GetRow())
{
$row['send_time'] = my_date_format($row['dateline'],"Y-m-d H:i");
$row['user'] = $row['msgfrom'];
$row['user_id'] = $row['msgfromid'];
$row['id'] = $row['pmid'];
$row['nickname'] = $row['msgnickname'];
$pm_list[]=$row;
}
$return['pms'] = $pm_list;
}
api_output($return);
}
function PmNew()
{
$uid = max(0,(int) $this->user['uid']);
$user = $this->_init_user($uid);
$text = trim(strip_tags($this->Inputs['text']));
if(strlen($text) <2)
{
api_error('text is empty',111);
}
$text = cutstr($text,300);
$to_user = trim(strip_tags($this->Inputs['to_user']));
if(!$to_user)
{
api_error('to_user is empty',112);
}
$_user = $this->DatabaseHandler->FetchFirst("select * from ".TABLE_PREFIX."members where `nickname`='{$to_user}'");
if(!$_user)
{
api_error('to_user is invalid',113);
}
$this->DatabaseHandler->SetTable(TABLE_PREFIX.'pms');
$data=array(
"msgfrom"=>$user['username'],
"msgnickname"=>$user['nickname'],
"msgfromid"=>$user['uid'],"msgtoid"=>$_user['uid'],"subject"=>$subject,
"message"=>$text,
"new"=>'1',
"dateline"=>time(),
);
$this->DatabaseHandler->Insert($data);
$this->DatabaseHandler->Query("update ".TABLE_PREFIX."members set `newpm`=`newpm`+1 where `uid`='{$_user['uid']}'");
api_output('send is ok');
}
}
?>